define([
    'app'
    , 'echarts'
    , 'echarts/config'
    //, 'echarts/chart/bar'
], function(webChart, EC, ecConfig) {

    var EventTable = {
        "click":        ecConfig.EVENT.CLICK
        , "dbclick":    ecConfig.EVENT.DBLCLICK
    };

    function constructEchart(element, name) {
        var chart = EC.init(element);
        require(["middleware/" + name], function(mid) {
            if (mid)    {
                for(var key in mid)     {
                    chart.on(EventTable[key], mid[key]);
                }
            }
        });
        return chart;
    };

    webChart.directive("echartPanel", function () {
        return {
            restrict:	"EA",
            replace:	 true,
            template:	"<div class='echart panel'></div>",
            scope: {
            },
            controller: function ($scope, $element) {
                var chart = null;

                $scope.$on("draw-data", function(e, data) {
                    var name = data.title.text;
                    require(['echarts/chart/bar'], function(Bar) {
                        chart || ( chart = constructEchart($element[0], name) );
                        chart.setOption(data);
                    })
                });

                // 自己测试
                BtwWpf.pushData(OPTION);
            },
            link:		function($scope, element, attrs, controllers) {
            }
        }
    });
});


var OPTION = {
    title:  {
        text:   "xxx"
    },
    tooltip: {
        show: true
    },
    legend: {
        data:['销量']
    },
    xAxis : [
        {
            type : 'category',
            data : ["衬衫","羊毛衫","雪纺衫","裤子","高跟鞋","袜子"]
        }
    ],
    yAxis : [
        {
            type : 'value'
        }
    ],
    series : [
        {
            "name":"销量",
            "type":"bar",
            "data":[5, 20, 40, 10, 10, 20]
        }
    ]
}
